<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Util_Data
 *
 * @author Young Suk Ahn
 */
class Util_Data {
	//put your code here
	public static function toMapptingArray(&$dataSource, $keyPropName, $valPropName)
	{
		$retval = array();
		foreach ($dataSource as $entry)
		{
			$retval[$entry->{$keyPropName}] = $entry->{$valPropName};
		}
		return $retval;
	}
	
	/**
	 * Depending on the object type, returns the value.
	 * E.g. if it is object, then returns the value of the property named key
	 * if it is array, returns the value of the value with index key
	 * @param type $dataSource
	 * @param type $key
	 * @param type $defValue
	 * @return null 
	 */
	public static function getValueOf(&$dataSource, $key, $defValue = "")
	{
		if ( is_object($dataSource) )
		{
			if ( property_exists($dataSource, $key) )
					return $dataSource->$key;
			return $defValue;
		}
		else if ( is_array($dataSource) )
		{
			if (array_key_exists($key, $dataSource) )
					return $dataSource[$key];
			return $defValue;
		}
		return null;
	}
	
	/**
	 * Return the value if not null, returns dafault value otherwise
	 * @param type $dataVal
	 * @param type $defValue
	 * @return type 
	 */
	public static function getValue($dataVal, $defValue = "")
	{
		if ($dataVal == NULL) {
			return $defValue;
		} 
		return $dataVal;
	}
	
	public static function hasBitFlagSet(&$dataSource, $key, $bit)
	{
		if ( is_object($dataSource) )
		{
			if ( property_exists($dataSource, $key) )
					return ($dataSource->$key & $bit) > 0 ;
		}
		else if ( is_array($dataSource) )
		{
			if (array_key_exists($key, $dataSource) )
					return ($dataSource[$key] & $bit) > 0;
		}
		return false;
	}
	
	public static function containsString($dataSource, $key)
	{
		return in_array($key, $dataSource);
	}
}

